package com.zhgxi.zf.service;

import java.util.List;

import javax.annotation.Resource;

import org.slave4j.orm.Filtration;
import org.slave4j.orm.Filtration.MatchType;
import org.slave4j.orm.hibernate.BaseDao;
import org.slave4j.orm.hibernate.BaseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zhgxi.zf.entity.Role;

@Service
@Transactional
public class RoleService extends BaseService<Role> {
	@Override
	@Resource(name = "roleDao")
	public void setBaseDao(BaseDao<Role> baseDao) {
		this.baseDao = baseDao;
	}

	public List<Role> findAllRole() {
		Filtration visible = new Filtration(MatchType.EQ, true, "visible");
		return baseDao.findList(visible);
	}

	public boolean deleteRole(Role r) {
		try {
			baseDao.unVisible(r.getId());
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public List<Role> findByRname(String rname) {
		Filtration filter = new Filtration(MatchType.LIKE, rname, "rolename");
		Filtration visible = new Filtration(MatchType.EQ, true, "visible");
		return baseDao.findList(filter, visible);
	}

	public boolean saveRole(Role role) {
		try {
			if (role.isNew() || role.getId() == 0) {
				baseDao.save(role);
			} else {
				baseDao.update(role);
			}
			return true;
		} catch (Exception e) {
			return false;
		}
	}
}
